package module.map.ImageResize
{
	import drongo.desktop.utils.BitmapDataUtil;
	import drongo.desktop.utils.StringUtil;
	import drongo.desktop.utils.UtilsFile;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.JPEGEncoderOptions;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.PNGEncoderOptions;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import common.view.FGUIMediator;
	import common.window.AlertDialog;
	
	import ui.Map.UI_ImageResizeWindow;
	
	public class ImageResizeWindowMediator extends FGUIMediator
	{
		[Inject]
		public var window:ImageResizeWindow;
		
		public function ImageResizeWindowMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.AddEvents();
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function AddEvents():void{
			this.view.m_closeButton.addClickListener(this.CloseButtonClick);
			this.view.m_button.addClickListener(this.StartButtonClick);
			this.view.m_selectBtn.addClickListener(this.SelectButtonClick);
		}
		
		private function RemoveEvents():void{
			this.view.m_closeButton.removeClickListener(this.CloseButtonClick);
			this.view.m_button.removeClickListener(this.StartButtonClick);
			this.view.m_selectBtn.removeClickListener(this.SelectButtonClick);
		}
		
		private function SelectButtonClick(e:Event):void{
			this.view.m_srcWidth.text="";
			this.view.m_srcHeight.text="";
			this.view.m_imagePath.text="";
			UtilsFile.browseForOpen("选择需要图片",[new FileFilter("image","*.png;*.jpg")],SelectedHandler);
		}
		
		private function SelectedHandler(file:File):void
		{
			this.view.m_imagePath.text=file.nativePath;
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,Preloading);
			loader.load(new URLRequest(this.view.m_imagePath.text));
		}
		
		private function Preloading(e:Event):void{
			var loadInfo:LoaderInfo=e.target as LoaderInfo;
			loadInfo.removeEventListener(Event.COMPLETE,Preloading);
			var loader:Loader=loadInfo.loader;
			var bitmap:Bitmap=loader.content as Bitmap;
			this.view.m_srcWidth.text=bitmap.width.toString();
			this.view.m_srcHeight.text=bitmap.height.toString();
		}
		
		private function ImageLoaded(e:Event):void{
			var loadInfo:LoaderInfo=e.target as LoaderInfo;
			loadInfo.removeEventListener(Event.COMPLETE,ImageLoaded);
			var loader:Loader=loadInfo.loader;
			var bitmap:Bitmap=loader.content as Bitmap;
			var bitmapData:BitmapData=bitmap.bitmapData;
			
			
			var targetSize:Number=Number(this.view.m_targetSize.text);
			var p:Number;
			//按照宽度来计算比例
			if(this.view.m_c1.selectedIndex==0){
				if(this.view.m_isWidthMode.selected){
					p=targetSize/bitmapData.width;
				}else{
					p=targetSize/bitmapData.height;
				}
			}else{
				p=Number(this.view.m_ratioInput.text);	
			}
			
			var newW:Number=bitmapData.width*p;
			var newH:Number=bitmapData.height*p;
			var newBitmapData:BitmapData=new  BitmapData(newW,newH,true);
			BitmapDataUtil.draw(newBitmapData,bitmapData,bitmapData.rect,new Point(),null,p);
			
			var byte:ByteArray=new ByteArray();
			
			var file:File=new File(this.view.m_imagePath.text);
			if(file.name.indexOf("png")>=0){
				newBitmapData.encode(newBitmapData.rect,new PNGEncoderOptions(),byte);
			}else{
				newBitmapData.encode(newBitmapData.rect,new JPEGEncoderOptions(80),byte);
			}
			UtilsFile.saveBytes(file,byte);
			
			this.CloseButtonClick();
		}
		
		private function StartButtonClick(e:Event):void{
			if(StringUtil.IsEmpty(this.view.m_imagePath.text)){
				AlertDialog.open(this.view.root,"图片地址不能为空！");
				return;
			}
			if(StringUtil.IsEmpty(this.view.m_targetSize.text)){
				AlertDialog.open(this.view.root,"目标大小不能为空!");
				return;
			}
			if(this.view.m_typeCB.selectedIndex==1){
				if(StringUtil.IsEmpty(this.view.m_ratioInput.text)){
					AlertDialog.open(this.view.root,"比例不能为空!");
					return;
				}
			}
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,ImageLoaded);
			loader.load(new URLRequest(this.view.m_imagePath.text));
		}
		
		private function CloseButtonClick(e:Event=null):void{
			this.window.hideImmediately();
			this.window.dispose();
			this.destroy();
		}
		
		private function get view():UI_ImageResizeWindow{
			return this.window.view;
		}
	}
}